<template>
  <div class="unqualified_review_manage">
    <!--QMS处理结论-->
    <problem-conclude ref="ProblemConclude" @submitSuccess="init"></problem-conclude>
    <!--操作记录-->
    <log-detail ref="logDetail"></log-detail>
    <one-title title="不合格品审理单管理" @refreshData="refresh">
      <div class="mes_btn">
        <el-button
          v-btn-permission="'synchro'"
          @click="handleSynchro">
          <i class="iconfont icon-btn_receive"></i>
          同步消息
        </el-button>
        <el-tooltip
          effect="dark"
          :content="concludeBtnStatus.content"
          :disabled="!concludeBtnStatus.status"
          popper-class="btn_tooltip"
          placement="bottom">
          <el-button
            v-btn-permission="'conclusionQMS'"
            type="success" @click="handleConclusionQMS" :class="{'is-disabled':concludeBtnStatus.status}">
            <i class="iconfont icon-btn_Save"></i>
            QMS处理结论
          </el-button>
        </el-tooltip>
      </div>
    </one-title>
    <div class="fixed_top_56">
      <mes-search-comp
        ref="mesSearch"
        :searchList="tableColConfig"
        :searchResult="searchCount"
        @searchInfo="_info">
      </mes-search-comp>
      <div class="table_container">
        <el-table
          border
          stripe
          ref="tableNode"
          v-loading="isLoading"
          :data="listData.list"
          :max-height="tableMaxHeight"
          :row-class-name="({row}) => !selectData.some((item) => item.id === row.id) || 'line_selected'"
          @row-click="row => $refs.tableNode.toggleRowSelection(row)"
          @sort-change="handleSortChange"
          @selection-change="handleSelectionChange"
          @filter-change="handleFilterChange">
          <el-table-column
            width="40"
            type="selection"
            align="center">
          </el-table-column>
          <mes-col-comp
            :key="index"
            v-for="(column, index) in columnList"
            :column="column">
          </mes-col-comp>
          <el-table-column
            label="操作"
            fixed="right"
            :resizable="false"
            show-overflow-tooltip
            align="center"
            min-width="160">
            <template slot-scope="scope">
              <div class="operation">
                <span aria-disabled="true" class="jump_item is_disabled">报告详情</span>
                <span @click="handleViewLog(scope.row)" class="jump_item">操作记录</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <mes-pagination
        @updateData="init"
        :currentPage="listData.pageNo"
        :total="listData.count || 0"
        :pageSize="listData.pageSize">
      </mes-pagination>
    </div>
  </div>
</template>

<script>
import tableColumns from './tableColumns'
import {ProblemConclude} from './components/index'

export default {
  components: {
    ProblemConclude,
  },
  name: 'UnqualifiedReviewManage',
  data() {
    return {
      tableColConfig: tableColumns, // 列配置
      isLoading: false, // 是否加载
      searchData: {}, // 查询参数
      sortData: {}, // 排序对象
      selectData: [], // 选择的列表
      searchCount: NaN, // 搜索结果数
    }
  },

  mounted() {
    this.init()
  },
  computed: {
    // 列表数据
    listData() {
      let getList = this.$store.state.unqualifiedReviewManage.listData
      if (getList.list) {
        getList.list.map(item => {
          this.$set(item, 'inputQuantity', item.inputQuantity || item.productionQuantity)
        })
      }
      return getList
    },
    // 周转状态
    batchStatus() {
      let batchStatus = this.$store.state.dict.batchStatus.labels || []
      return this.addLabelAll(batchStatus)
    },
    // 列循环
    columnList() {
      return this.tableColConfig.filter(item => item.noForCol !== true && item.isShow !== false)
    },
    //QMS处理结论
    concludeBtnStatus() {
      let status = false
      if (this.selectData.length > 1) {
        status = true
        return {status: status, content: '请选择一条数据'}
      }
      if (this.selectData.some((item) => item.qmsProcessStatusValue !== 'Finished')) {
        status = true
      }
      if (this.selectData.some((item) => item.isCreateTurnoverValue !== 'False')) {
        status = true
      }
      return {
        status: status,
        content: '请选择审理状态为"已审理"且已创建任务为"否"的数据'
      }
    }
  },
  methods: {
    // 列筛选回调
    handleFilterChange(filters) {
      let filterParams = this.$filterParams(this.tableColConfig, filters)
      Object.assign(this.searchData, filterParams.search)
      this.sortData = filterParams.order // 排序对象
      this.$refs.tableNode.clearSort()
      this.init()
    },
    // 刷新
    refresh() {
      this.searchData = {} // 查询参数
      this.sortData = {} // 排序对象
      this.$refs.mesSearch.clean()
      this.$refs.tableNode.clearSort()
      this.$refs.tableNode.clearFilter()
      this.init()
    },
    // 初始化方法
    init(page) {
      this.isLoading = true
      let postData = {
        method: 'post',
        url: 'a/workstageproblemqms/workstageProblemQMS/list',
        module: 'unqualifiedReviewManage',
        data: {
          pageNo: page ? page.pageNo : 1,
          pageSize: page ? page.pageSize : this.listData.pageSize || 15,
        },
        state: 'listData'
      }
      if (Object.keys(this.searchData).length > 0) {
        postData.data.query = this.searchData
      }
      if (Object.keys(this.sortData).length > 0) {
        postData.data.sort = this.sortData
      }
      this.$store.dispatch('saveData', postData).then((res) => {
        if (Object.keys(this.searchData).length > 0) {
          this.searchResult = res.count
        } else {
          this.searchResult = NaN
        }
      }).finally(() => {
        this.isLoading = false
      })
    },
    getIds() {
      return this.selectData.reduce((pre, cur) => [...pre, cur.id], [])
    },
    // 同步消息
    handleSynchro() {
      if (!this.selectData.length) {
        this.tipMessage('error', '未选择任何数据')
        return true
      } else {
        this.mesConfirm('选中的记录将被进行同步消息操作, 是否继续?', '同步消息提示').then(() => {
          this.$store.dispatch('saveData', {
            method: 'post',
            url: 'a/workstageproblemprocess/workstageProblemProcess/synchroniseqmsmessage',
            data: {
              problemQMSIds: this.getIds()
            },
            noCommit: true
          }).then(() => {
            this.init()
          })
        })
      }
    },
    // QMS处理结论
    handleConclusionQMS() {
      if (!this.selectData.length) {
        return this.tipMessage('error', '请选择需要进行QMS处理结论的数据')
      }
      if (this.concludeBtnStatus.status) return
      this.$refs.ProblemConclude.openDialog(this.selectData)
    },
    // 操作记录
    handleViewLog(row) {
      // this.$refs.ViewLog.openDialog(row)
      this.$refs.logDetail.openDialog({
        title: '周转码',
        name: row.turnoverNumber,
        url: 'a/turnoverstatuschangehistory/turnoverStatusChangeHistory/list',
        query: {
          turnoverBatchId: row.turnoverId
        }
      })
    },
    // 多选
    handleSelectionChange(val) {
      this.selectData = val
    },
    // 排序方法
    handleSortChange(e) {
      this.sortData = this.$getSortOrder(e)
      this.init()
    },
    // 搜索方法
    _info(data) {
      this.searchData = data
      this.$refs.tableNode.clearFilter()
      this.$refs.tableNode.clearSort()
      this.sortData = {}
      this.init()
    },
    /**
     * 提示
     * @param str 提示内容
     */
    tipMessage(type, str) {
      this.$message({
        type: type,
        message: str,
        duration: 1500,
        center: true
      })
    }
  }
}
</script>

<style scoped lang="less">
  .unqualified_review_manage {
    .operation {
      display: flex;
      align-items: center;
      justify-content: space-around;
    }
  }
</style>
